﻿using PusherClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebsocketClientDemoByLaravelWebsocket
{

    
    public partial class Form1 : Form
    {
       public bool isConnected = false;
        
        public Form1()
        {
            InitializeComponent();
        }

        private async void Button1_Click(object sender, EventArgs e)
        {
            string host = WSHost.Text;
            string appKey = AppKey.Text;
            string ChannelName = Channel.Text;
            string key = AppKey.Text;
            string Event = EventName.Text;
            try
            {
                //Creating pusher object with authentication
                Pusher pusher = new Pusher(key, new PusherOptions
                {
                    Host = host,
                });

               

                await pusher.ConnectAsync().ConfigureAwait(false);
                //Subscribing to channel
                Channel channel = await pusher.SubscribeAsync(ChannelName).ConfigureAwait(false);
                if (channel.IsSubscribed)
                {
                    string msg = "连接成功！";
                    MessageBox.Show(msg);
                    Console.WriteLine(msg);
                    channel.Bind(Event, (PusherEvent eventResponse) =>
                    {
                        MessageBox.Show("检测到服务器数据返回，请查看控制台");
                        Console.WriteLine(eventResponse.Data);
                    });
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
                Console.WriteLine(e1.ToString());
            }
        }
    }
}
